package com.js.baseview.selector;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.js.baseview.R;
import com.js.baseview.selector.adapter.SelectionAdapter;
import com.js.baseview.selector.entity.SelectionItem;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by viroyal002 on 2016/7/22.
 * 下拉菜单
 */
public class SearchListPopupWindow {
    private PopupWindow mListPopupwindow;
    private ListView mListView;
    private EditText etSearch;

    private List<SelectionItem> mItems;
    private List<SelectionItem> showingItems;
    private SelectionAdapter mAdapter;

    /**
     * 初始化下拉菜单
     *
     * @param context
     * @param width   popupwindow的宽
     * @param height  popupwindow的高
     */
    public void initPopWindow(final Context context, int width, int height, List<SelectionItem> items, String hint) {
        View view = LayoutInflater.from(context).inflate(R.layout.popup_list_search, null, false);
        etSearch = view.findViewById(R.id.et_search);
        mListView = (ListView) view.findViewById(R.id.popup_item);
        mListPopupwindow = new PopupWindow(view, width, height);
        mListPopupwindow.setBackgroundDrawable(new BitmapDrawable());
        mListPopupwindow.setFocusable(true);
        mListPopupwindow.setOutsideTouchable(true);
        mListPopupwindow.setAnimationStyle(R.style.MyPopupWindow_anim_style);

        //解决popupwindow上button的焦点问题
        mListPopupwindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    mListPopupwindow.dismiss();
                    return true;
                }
                return false;
            }
        });

        this.mItems = items;
        this.showingItems = items;
        mAdapter = new SelectionAdapter(context, showingItems);
        mListView.setAdapter(mAdapter);

        etSearch.setHint(hint);
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                List<SelectionItem> newItems = new ArrayList<>();
                for (int i = 0; i < mItems.size(); i++) {
                    SelectionItem item = mItems.get(i);
                    String data = item.getData();
                    if (data.contains(s.toString())) {
                        newItems.add(item);
                    }
                }
                Log.d("SearchList", newItems.toString());
                showingItems = newItems;
                mAdapter = new SelectionAdapter(context, showingItems);
                mListView.setAdapter(mAdapter);
            }
        });
    }

    public List<SelectionItem> getShowingItems() {
        return showingItems;
    }

    //获取popupwindow对象
    public PopupWindow getListPopupWindow() {
        return mListPopupwindow;
    }

    //设置popupwindow的onDismiss事件
    public void setOnDismissListener(PopupWindow.OnDismissListener listener) {
        if (listener != null) {
            mListPopupwindow.setOnDismissListener(listener);
        }
    }

    //设置popupwindow的位置(指定view的下方)
    public void showAsDropDown(View view) {
        mListPopupwindow.showAsDropDown(view);
    }

    //隐藏popupwindow
    public void disMissPopup() {
        if (mListPopupwindow.isShowing()) {
            mListPopupwindow.dismiss();
        }
    }

    public boolean isShowing() {
        return mListPopupwindow.isShowing();
    }

    //获取popupwindow的listview对象
    public ListView getListView() {
        return mListView;
    }

    //为listview设置adapter
    public void setAdapter(BaseAdapter adapter) {
        if (adapter != null) {
            mListView.setAdapter(adapter);
        }
    }

    //设置listview的子项点击事件
    public void setOnListItemClickListener(AdapterView.OnItemClickListener listener) {
        if (listener != null) {
            mListView.setOnItemClickListener(listener);
        }
    }
}
